<?php

class AdminTeacher_Controller extends Admin_Controller
{
    public function index(Request $request, Response $response) {
        $size = intval($request->size) ?: 30;
        $p = intval($request->p ?: 1);
        $from = ($p - 1) * $size;
        $q = new TrueQuery();
        if ($request->search) $q = new OrQuery(new SearchQuery('name', $request->search), new SearchQuery('teacherNo', $request->search));
        $data = Teacher::find($q, ['from' => $from, 'size' => $size, 'sort' => ['created' => 'ASC']]);
        $page_nav['count'] = Teacher::totalCount($q);
        $nav = new SubPages($size, $page_nav['count'], $p, 10, "/admin/Teacher/?p=",2);
        $page_nav['nav'] = $nav->subPageCss2();
        $response->append(new View('admin/teacher/list', compact('data', 'page_nav')));
    }
    public function edit(Request $request, Response $response) {
        if (!$request->isPost()) {
            $id = $request->path(3);
            $data = Teacher::load($id);
            if (!$id || !$data) $data = new Teacher();
            $response->send(new View('admin/teacher/edit', compact('data')));
        }
        /**
         * @var	Teacher	$data
         * @var User    $user
         */
        if (Teacher::load($request->id)) $originalNo = Teacher::load($request->id)->teacherNo;
        $data = Teacher::createByRequest($request);
        $nullCheck = [
            'name' => '教师姓名',
            'sex' => '教师性别',
        ];
        foreach ($nullCheck as $item => $description) if (!$data->$item) $response->alertErrorAndGoBack("{$description}不能为空！");
        $isNew = $data->id ? false : true;
        $data->hireTime = strtotime($data->hireTime);
        if ($isNew) {
            $duplicateCheck = [
                'teacherNo' => '工号',
            ];
            foreach ($duplicateCheck as $item => $description) if (Teacher::totalCount(new Query($item, $data->$item)) != 0) $response->alertErrorAndGoBack("已有{$item}为{$description}的记录存在!不允许重复!");
        }
        $data->save();
        if ($isNew) {
            $user = new User();
            $user->internalId = $data->id;
            $user->name = $data->teacherNo;
            $user->password = '';
            $user->type = User::TYPE_TEACHER;
            $user->save();
            $user->password = $user->generatePassword($data->teacherNo);
            $user->save();
            $response->alertAndGoTo('操作成功!默认登陆用户名和密码均为工号!', '/admin/Teacher/');
        }
        if ($originalNo && $originalNo != $data->teacherNo) {
            $user = array_values(User::find(new Query('name', $originalNo)))[0];
            $user->name = $data->teacherNo;
            $user->save();
            $response->alertAndGoTo('操作成功!登陆用户名已修改为新工号!', '/admin/Teacher/');
        }
        $response->alertAndGoTo('操作成功!', '/admin/Teacher/');
    }
    public function delete(Request $request, Response $response) {
        if (!$request->path(3)) $response->alertErrorAndGoBack('请求无效！教师不存在！');
        /**
         * @var	Teacher	$teacher
         * @var CourseClass $courseClass
         * @var CourseSelection $courseSelection
         */
        if (!($teacher = Teacher::load($request->path(3)))) $response->alertAndGoBack('该教师不存在！');
        $courseClasses = CourseClass::find(new Query('teacherId', $teacher->id));
        foreach ($courseClasses as $courseClass) {
            $courseSelections = CourseSelection::find(new Query('courseClassId', $courseClass->id));
            foreach ($courseSelections as $courseSelection) $courseSelection->delete();
            $courseClass->delete();
        }
        $teacher->delete();
        $teacher->user()->delete();
        $response->alertAndGoBack('删除成功！所有与该教师相关的记录都已删除!');
    }
}